<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $leftmenuList = getMenus(null,true,0);
        $login_user_company_level = 0;
        $this->assign('leftMenux',$leftmenuList);
    	$this->display();
    }
    public function indexLayout(){
        $tbHtml = '';
        $tbHtml .= '<style>td{padding:5px;border-bottom: 1px solid #000;}</style>';
    	$sum = M('PrepaidCard')->field("sum(pc_value) as sum")->where(array('pc_is_use'=>1))->find();
        $tbHtml .= '<h1>用户充值总数：'.$sum['sum'].'</h1>';
        $versionList = M('Version')->field("ve_value,(select count(*) as datanum from tb_device where dev_version_id = ve_id and dev_status = 1) as datanum")->select();

        $tbHtml .= '<table><thead>';
        $tbHtml .= '<tr>';
        $tbHtml .= '<td>'.'<strong>版本信息</strong>'.'</td><td>'.'<strong>空闲数量</strong>'.'</td>';
        $tbHtml .= '</tr></thead><tbody>';
        foreach ($versionList as $key => $value) {
            $tbHtml .= '<tr>';
            $tbHtml .= '<td>'.$value['ve_value'].'</td><td>'.$value['datanum'].'</td>';
            $tbHtml .= '</tr>';
        }
        $tbHtml .= '</tbody></table>';
        echo $tbHtml;
    }
    public function login(){
        if(IS_POST){
            if(I('post.account')&&I('post.password')){
                if(!check_verify(I('post.verifyCode'))){$this->error("验证码错误！");}
            }else{$this->error("账号,密码不能为空！");}
            $login_info = array(
                'ad_account'=>$_POST['account'],
                'ad_password'=>md5($_POST['password'])
            );
            $data = M('Admin')->where($login_info)->find();
            if(!empty($data)){
                session("admin", $data);
                $this->success("登陆成功！");
            }else{
                $this->error("账号或密码错误！");
            }
        }else{
            $this->display();
        }
    }
    public function edit_user_info(){
        if(IS_POST){
            $check_data = array(
                'password'=>md5(I('post.oldPasswd')),
                'newPasswd'=>md5(I('post.newPasswd')),
            );
            $admin = session("admin");
            $data = M('Admin')->where($admin)->find();
            if($admin['ad_password']!=$check_data['password']){
                exit(error("密码错误！"));
            }else{
                $admin['ad_password'] = $check_data['newPasswd'];
                M('Admin')->save($admin);
                session("admin",$admin);
                exit(success("修改成功！",true));
            }
        }else{
            $this->display();
        }
    }
    public function quit(){
        session('user',null);
        $this->redirect("Index/login");
    }
    public function verify(){
        return verify();
    }
}